<?php

/**
 * Contact Component 
 */
class BannerController extends AController {

    public function actions() {
        return array(
            'upload' => array(
                'class'  => 'ext.actions.upload.PlUploadImage',
                'file_name' => 'file_upload',
                'params' => Yii::app()->params['banner'],
            ),
        );
    }
    
    public function actionIndex() {
        $model = new DBanner;
        $data = $model->getContent();
        
        $this->render('index', array(
            'model' => $model,
            'data' => $data,
        ));
    }
    
    public function actionCreate() {
        $model = new DBanner('insert');
        
        if (isset($_POST['DBanner'])) {
            $model->attributes = $_POST['DBanner'];
            if ($model->validate() && $model->save()) {
                Yii::app()->user->setFlash('message', 'created');
                $this->redirect(array('index'));
            }
        } else {
            if (Yii::app()->user->hasState($model->params['plupload_session'])) {
                $session      = Yii::app()->user->getState($model->params['plupload_session']);
                $model->file  = $session['file_name'];
                $model->thumb = $session['thumb_name'];
            }
        }
        
        $this->render('create', array(
            'model' => $model,
        ));
    }
    
    public function actionUpdate($id='') {
        $model = new DBanner('update');
        if (! $model->find($id))
            throw new CHttpException('403', 'Forbidden');

        if (isset($_POST['DBanner'])) {
            $model->attributes = $_POST['DBanner'];
            
            if ($model->validate() && $model->save()) {
                Yii::app()->user->setFlash('message', 'updated');
                $this->redirect(array('index'));
            }
        }
        
        $this->render('update', array(
            'model' => $model,
        ));
    }
    
    public function actionDelete($id) {
        $this->detectAjaxRequest();
        
        $model = new DBanner('delete');
        if ($model->delete($id)) {
            Yii::app()->user->setFlash('message', 'deleted');
            echo 'success';
        } else {
            echo 'fail';
        }
    }
    
    public function actionLoadMyGrid() {
        $this->detectAjaxRequest();
        
        $model = new DBanner();
        $data = $model->getContent();

        $this->renderPartial('_grid', array(
            'model' => $model,
            'data' => $data,
        ));
    }
    
    public function actionToggle($key, $id) {
        $this->detectAjaxRequest();
        
        $model = new DBanner('toggle');
        if ($model->toggleColumn($key, $id)) {
            echo 'success';
        } else {
            echo 'fail';
        }
    }
    
    public function actionMoveRecord($direct, $id) {
        $this->detectAjaxRequest();
        
        $model = new DBanner('toggle');
        if ($model->sortOrder($direct, $id)) {
            echo 'success';
        } else {
            echo 'fail';
        }
    }
    
    public function actionCancel() {
        $this->detectAjaxRequest();
        $model = new DBanner();
        $model->clearSession();
    }
    
    private function detectAjaxRequest() {
        if (! Yii::app()->request->isAjaxRequest) {
            throw new CHttpException('403', 'Forbidden');
            Yii::app()->end();
        }
    }

}